package com.kensure.shike.baobei.service;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKKanJiaDetailDao;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKKanJia;
import com.kensure.shike.baobei.model.SKKanJiaDetail;
import com.kensure.shike.baobei.service.SKKanJiaDetailService;
import com.kensure.shike.user.service.SKUserService;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.ArithmeticUtils;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

/**
 * 参与砍价的详情 服务实现类
 * 
 * @author fankd created on 2019-7-1
 * @since
 */
@Service
public class SKKanJiaDetailService extends JSBaseService {

	@Resource
	private SKKanJiaDetailDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private SKKanJiaService sKKanJiaService;
	@Resource
	private SKUserService sKUserService;

	public SKKanJiaDetail selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKKanJiaDetail> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKKanJiaDetail> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKKanJiaDetail obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_kanjia_detail"));
		return dao.insert(obj);
	}

	public boolean update(SKKanJiaDetail obj) {
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 获取砍价过的成员列表
	 * 
	 * @param kjid
	 * @return
	 */
	public List<SKKanJiaDetail> getKanJia(Long kjid) {
		Map<String, Object> parameters = MapUtils.genMap("kjid", kjid);
		List<SKKanJiaDetail> list = selectByWhere(parameters);
		if (CollectionUtils.isNotEmpty(list)) {
			for (SKKanJiaDetail det : list) {
				det.setUser(sKUserService.selectOne(det.getUserid()));
			}
		}

		return list;
	}

	/**
	 * 获取该成员是否砍过价
	 * 
	 * @param kjid
	 * @return
	 */
	public SKKanJiaDetail getKanJia(Long kjid, Long userid) {
		Map<String, Object> parameters = MapUtils.genMap("kjid", kjid, "userid", userid);
		List<SKKanJiaDetail> list = selectByWhere(parameters);
		SKKanJiaDetail det = null;
		if (CollectionUtils.isNotEmpty(list)) {
			det = list.get(0);
		}

		return det;
	}

	/**
	 * 参与砍价
	 * 
	 * @param userid
	 * @param bbid
	 * @return
	 */
	public synchronized SKKanJiaDetail add(Long kjid, Long userid) {
		SKKanJiaDetail det = getKanJia(kjid, userid);
		if (det != null) {
			BusinessExceptionUtil.threwException("您已经参与过砍价了");
		}
		SKKanJia kanjia = sKKanJiaService.selectOne(kjid);
		SKBaobei baobei = sKBaobeiService.selectFull(kanjia.getBbid());
		
		Date now = new Date();
		if(now.compareTo(baobei.getTimeExpire())>0){
			BusinessExceptionUtil.threwException("该商品砍价活动已经结束");
		}

		if (kanjia.getStatus() != 1) {
			BusinessExceptionUtil.threwException("砍价已结束");
		}
		// 基数
		double jishu = ArithmeticUtils.sub(kanjia.getCurPrice(), baobei.getMinPrice());
		double curPrice = jishu;
		if (baobei.getKjcs() >= kanjia.getKjcs()) {
			Random rand = new Random();
			int j = rand.nextInt(5);
			double beishu = ArithmeticUtils.div(2 + j, 100);
			curPrice = ArithmeticUtils.mul(jishu, beishu, 2);
		}

		SKKanJiaDetail kanjiadet = new SKKanJiaDetail();
		kanjiadet.setKjid(kjid);
		kanjiadet.setUserid(userid);
		kanjiadet.setCurPrice(curPrice);
		insert(kanjiadet);

		// 砍价情况
		kanjia.setCurPrice(ArithmeticUtils.sub(kanjia.getCurPrice(), curPrice));
		kanjia.setKjcs(kanjia.getKjcs() + 1);
		sKKanJiaService.update(kanjia);

		return kanjiadet;
	}

}
